package com.rjwh.dingdong.client.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;

import com.example.whrjwhbase.R;
import com.rjwh.dingdong.client.util.LocalConstant;

/**
 * 表情选择栏
 * 
 * @ClassName FaceRootView
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author 王小珂
 * @date 2016-4-28 下午3:11:03
 * @history 1.YYYY-MM-DD author: description:
 */
public class FaceRootView extends LinearLayout {

	private ImageView point0;
	private ImageView point1;
	private ImageView point2;
	private ArrayList<Map<String, Object>> emos;
	private ArrayList<String> emoNames;
	private onFaceViewListener listener;

	public void setOnFaceViewListener(onFaceViewListener listener) {
		this.listener = listener;
	}

	public interface onFaceViewListener {
		/**
		 * 显示表情符
		 * 
		 * @Title: showFaceView
		 * @Description: TODO(这里用一句话描述这个方法的作用)
		 * @param @param key
		 * @return void
		 * @throws
		 * @history 1.YYYY-MM-DD author: description:
		 */
		void showFaceView(String key);
	}

	public FaceRootView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public FaceRootView(Context context) {
		super(context);
		init();
	}

	private void init() {
		View view = inflate(getContext(), R.layout.face_root_view, null);
		addView(view);
		ViewPager faceListViewPager = (ViewPager) view
				.findViewById(R.id.face_list_viewpager);
		point0 = (ImageView) findViewById(R.id.page0_select);
		point1 = (ImageView) findViewById(R.id.page1_select);
		point2 = (ImageView) findViewById(R.id.page2_select);
		initEmoView(faceListViewPager);
	}

	/**
	 * @param faceListViewPager
	 *            初始化表情布局
	 * 
	 * @Title: initEmoView
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 */
	private void initEmoView(ViewPager faceListViewPager) {
		emos = new ArrayList<Map<String, Object>>();
		emoNames = new ArrayList<String>();
		for (int i = 0; i < LocalConstant.expressionImgs.length; i++) {
			Map<String, Object> listItem = new HashMap<String, Object>();
			listItem.put("image", LocalConstant.expressionImgs[i]);
			emos.add(listItem);
			emoNames.add(LocalConstant.expressionImageNames[i]);
		}
		List<View> views = new ArrayList<View>();
		for (int i = 0; i < 3; ++i) {
			views.add(getGridView(i));
		}
		if (isInEditMode()) {
			return;
		}
		faceListViewPager.setAdapter(new EmoViewPagerAdapter(views));
		faceListViewPager.addOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					point0.setImageDrawable(getResources().getDrawable(
							R.drawable.page_select_dot));
					point1.setImageDrawable(getResources().getDrawable(
							R.drawable.page_nomal_dot));

					break;
				case 1:
					point1.setImageDrawable(getResources().getDrawable(
							R.drawable.page_select_dot));
					point0.setImageDrawable(getResources().getDrawable(
							R.drawable.page_nomal_dot));
					point2.setImageDrawable(getResources().getDrawable(
							R.drawable.page_nomal_dot));
					break;
				case 2:
					point2.setImageDrawable(getResources().getDrawable(
							R.drawable.page_select_dot));
					point1.setImageDrawable(getResources().getDrawable(
							R.drawable.page_nomal_dot));
					point0.setImageDrawable(getResources().getDrawable(
							R.drawable.page_nomal_dot));
					break;
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private View getGridView(final int i) {
		View view = View.inflate(getContext(), R.layout.grid_emos, null);
		GridView gridview = (GridView) view.findViewById(R.id.gridview);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		final List<String> nameList = new ArrayList<String>();
		if (i == 0) {
			list.addAll(emos.subList(0, 24));
			nameList.addAll(emoNames.subList(0, 24));
		} else if (i == 1) {
			list.addAll(emos.subList(24, 48));
			nameList.addAll(emoNames.subList(24, 48));
		} else if (i == 2) {
			list.addAll(emos.subList(48, emos.size()));
			nameList.addAll(emoNames.subList(48, emoNames.size()));
		}
		final SimpleAdapter simpleAdapter = new SimpleAdapter(getContext(),
				list, R.layout.singleexpression, new String[] { "image" },
				new int[] { R.id.face_item_image });
		if (!isInEditMode()) {
			gridview.setAdapter(simpleAdapter);
			gridview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int position, long arg3) {
					String key = nameList.get(position);
					listener.showFaceView(key);
				}
			});
		}
		return view;
	}

	/**
	 * 表情Viewpage数据源
	 * 
	 * @ClassName EmoViewPagerAdapter
	 * @Description TODO(这里用一句话描述这个类的作用)
	 * @author 王博扬
	 * @date 2015-9-15 下午4:32:00
	 * @history 1.YYYY-MM-DD author: description:
	 */
	public class EmoViewPagerAdapter extends PagerAdapter {

		private List<View> views;

		public EmoViewPagerAdapter(List<View> views) {
			this.views = views;
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(views.get(arg1));
			return views.get(arg1);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(views.get(arg1));

		}

	}
}
